﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;

namespace LOPA
{
    public partial class RiskCriteria : UserControl
    {
        public RiskCriteria()
        {
            InitializeComponent();
            _bindSafe.DataSource = Program.Workspace.LopaProject.RiskCriteria.SafeCriterias;
            _bindEnv.DataSource = Program.Workspace.LopaProject.RiskCriteria.EnvCriterias;
            _bindBusiness.DataSource = Program.Workspace.LopaProject.RiskCriteria.BusinessCriterias;
            dgvSafe.DataSource = _bindSafe;
            dgvEnv.DataSource = _bindEnv;
            dgvBusiness.DataSource = _bindBusiness;
        }

        private void dgvSafe_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgvSafe.RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgvSafe.RowHeadersDefaultCellStyle.Font, rectangle,
                dgvSafe.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
        private void dgvEnv_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgvEnv.RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgvEnv.RowHeadersDefaultCellStyle.Font, rectangle,
                dgvEnv.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
        private void dgvBusiness_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgvBusiness.RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgvBusiness.RowHeadersDefaultCellStyle.Font, rectangle,
                dgvBusiness.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void addNewRow_Click(object sender, EventArgs e)
        {
            if (!dgvSafe.Enabled){
                return;
            }
            
            RiskCriteriaItem item = new RiskCriteriaItem();
            _bindSafe.Insert(dgvSafe.Rows.Count,item);
            dgvSafe.Rows[dgvSafe.Rows.Count - 1].Selected = true;
            dgvSafe.CurrentCell = dgvSafe[1, dgvSafe.Rows.Count - 1];
            List<string> newSafeList = new List<string>();
            foreach (RiskCriteriaItem criteriaItem in Program.Workspace.LopaProject.RiskCriteria.SafeCriterias)
            {
                if (criteriaItem.SafeType != null)
                {
                    newSafeList.Add(criteriaItem.SafeType);
                }
            }
            Program.Workspace.OnSafeListChanged(newSafeList);
        }

        private void _btnEdit_Click(object sender, EventArgs e){
            dgvSafe.Enabled = true;
            dgvEnv.Enabled = true;
            dgvBusiness.Enabled = true;
        }

        private void _btnSave_Click(object sender, EventArgs e)
        {
            dgvSafe.Enabled = false;
            dgvEnv.Enabled = false;
            dgvBusiness.Enabled = false;
            Program.Workspace.SaveProject(MainForm.mainForm.SaveFilePath);
            MessageBox.Show("风险标准保存成功!");
        }
        public void SaveWithoutRemider()
        {
            dgvSafe.Enabled = false;
            dgvEnv.Enabled = false;
            dgvBusiness.Enabled = false;
        }

        private void addNewEnv_Click(object sender, EventArgs e)
        {
            if (!dgvEnv.Enabled)
            {
                return;
            }
            RiskCriteriaItem item = new RiskCriteriaItem();
            _bindEnv.Insert(dgvEnv.Rows.Count, item);
            dgvEnv.Rows[dgvEnv.Rows.Count - 1].Selected = true;
            dgvEnv.CurrentCell = dgvEnv[1, dgvEnv.Rows.Count - 1];
            List<string> newSafeList = new List<string>();
            foreach (RiskCriteriaItem envCriteria in Program.Workspace.LopaProject.RiskCriteria.EnvCriterias)
            {
                if (envCriteria.SafeType != null)
                {
                    newSafeList.Add(envCriteria.SafeType);
                }
            }
            Program.Workspace.OnSafeListChanged(newSafeList);
        }

        private void addNewBusiness_Click(object sender, EventArgs e)
        {
            if (!dgvBusiness.Enabled)
            {
                return;
            }
            RiskCriteriaItem item = new RiskCriteriaItem();
            _bindBusiness.Insert(dgvBusiness.Rows.Count, item);
            dgvBusiness.Rows[dgvBusiness.Rows.Count - 1].Selected = true;
            dgvBusiness.CurrentCell = dgvBusiness[1, dgvBusiness.Rows.Count - 1];
            List<string> newSafeList = new List<string>();
            foreach (RiskCriteriaItem businessCriteria in Program.Workspace.LopaProject.RiskCriteria.BusinessCriterias)
            {
                if (businessCriteria.SafeType != null)
                {
                    newSafeList.Add(businessCriteria.SafeType);
                }
            }
            Program.Workspace.OnSafeListChanged(newSafeList);
        }

        private void defaultToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(new WarringForm("确认将恢复默认风险标准设置？").ShowDialog() == DialogResult.OK){
                Program.Workspace.LopaProject.RiskCriteria = new Model.RiskCriteria();
                _bindSafe.DataSource = Program.Workspace.LopaProject.RiskCriteria.SafeCriterias;
                _bindEnv.DataSource = Program.Workspace.LopaProject.RiskCriteria.EnvCriterias;
                _bindBusiness.DataSource = Program.Workspace.LopaProject.RiskCriteria.BusinessCriterias;
            }
        }

    }
}
